package com.sidecar.service.log.web;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;

import com.sidecar.common.dto.search.Pagination;
import com.sidecar.common.dto.search.SearchRequest;
import com.sidecar.common.dto.vo.base.ResponseData;
import com.sidecar.common.dto.vo.log.request.OperationLogReq;
import com.sidecar.service.log.service.OperationLogService;

import reactor.core.publisher.Mono;

@Component
public class OperationLogController {

    @Resource
    OperationLogService operationLogService;

    public Mono<ServerResponse> create(final ServerRequest req) {
        operationLogService.create(req.bodyToMono(OperationLogReq.class).block());
        return ServerResponse.ok().body(Mono.justOrEmpty(ResponseData.ok()), ResponseData.class);
    }

    public Mono<ServerResponse> removeById(final ServerRequest request) {
        operationLogService.removeById(Long.valueOf(request.pathVariable("id")));
        return ServerResponse.ok().body(Mono.justOrEmpty(ResponseData.ok()), ResponseData.class);
    }

    public Mono<ServerResponse> search(final ServerRequest req) {
        return ServerResponse.ok().body(
                Mono.justOrEmpty(operationLogService.search(req.bodyToMono(SearchRequest.class).block())),
                Pagination.class);
    }

}
